<?php
declare(strict_types=1);

namespace App\Service;

use App\Model\Enum\CommonReviewStatusEnum;
use App\Utils\CustomUtils;
use Hyperf\Di\Annotation\Inject;
use App\Model\Dao\MchRegEmailChangeApplyDao;
use App\Model\Dao\MchInfoDao;
use App\Exception\XZXValidationException;
use App\Constants\XZXResponseCode;
use Hyperf\Utils\ApplicationContext;
use Hyperf\Utils\Coroutine;
use App\Utils\DateUtils;
use App\Constants\StrStatus;
use App\Service\EmailService;

class MchRegEmailChangeApplyService
{

    /**
     * @Inject()
     * @var EmailService
     */
    private $EmailService;

    /**
     * @Inject()
     * @var MchInfoDao
     */
    private $MchInfoDao;

    /**
     * @Inject()
     * @var MchRegEmailChangeApplyDao
     */
    private $MchRegEmailChangeApplyDao;

    public function get($param)
    {
        $result = $this->MchRegEmailChangeApplyDao->get($param['id']);
        if (!$result) {
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::DATA_NOT_FOUND));
        }

        if($param['source'] == 2 && !$param['isSuperRole']){
            if(!in_array($result['mchId'],$param['mchIdList'])){
                throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::NO_PERMISSION_TO_OPERATE));
            }
        }

        (isset($result['createTime']) && !empty($result['createTime'])) && $result['createTime'] = DateUtils::getDateTime($result['createTime']);
        (isset($result['reviewTime']) && !empty($result['reviewTime'])) && $result['reviewTime'] = DateUtils::getDateTime($result['reviewTime']);


        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE),$result);
    }

    public function del($param)
    {
        $result = $this->MchRegEmailChangeApplyDao->get($param['id']);
        if (!$result) {
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::DATA_NOT_FOUND));
        }
        
        try {
            $result = $this->MchRegEmailChangeApplyDao->del($param['id'],false);
        } catch (\Throwable $th) {
            CustomUtils::xzxDebug($th->getMessage());
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::DEL_FAIL));
        }
        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE),CustomUtils::xzxEmptyObject());
    }

    public function add($param)
    {
        // TODO: Implement add() method.
        //$data            =  [
        //    'name'       => $param['name'],
        //    'desciption' => $param['desciption'],
        //
        //];
        $data = $param;
        CustomUtils::xzxDebug($data);
        
        try {
            $this->MchRegEmailChangeApplyDao->insert($data);
        } catch (\Throwable $th) {
            CustomUtils::xzxDebug($th->getMessage());
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::ADD_FAIL));
        }
        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE),CustomUtils::xzxEmptyObject());
    }

    public function edit($param)
    {
        $info = $this->MchRegEmailChangeApplyDao->get($param['id']);
        if (!$info) {
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::DATA_NOT_FOUND));
        }

        if($param['source'] == 2 && !$param['isSuperRole']){
            if(!in_array($info['mchId'],$param['mchIdList'])){
                throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::NO_PERMISSION_TO_OPERATE));
            }
        }

        // 检查
        $mch = $this->MchInfoDao->get($info['mchId']);
        if (empty($mch)) {
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::NO_MERCHANT));
        }
        

        $id = $param['id'];
        $data = array();
                
        foreach($param as $k => $v){
            if($k == "id" || $k == "status"){
                continue;
            }     
            if($k == 'reviewStatus' && $param['source'] == 2){
                $data['reviewBy'] = $param['createBy'];
                $data['reviewTime'] = time();
            }
            if(isset($v)){
                $data[$k] = $v;
            }
        }
        unset($data['isSuperRole']);
        unset($data['mchIdList']);
        CustomUtils::xzxDebug($data);
        
        try {
            $this->MchRegEmailChangeApplyDao->update($id, $data);
            if($data['reviewStatus'] == 1){
                $mchData['email'] = $info['newEmail'];
                $this->MchInfoDao->update($info['mchId'], $mchData);
            }
            
        } catch (\Throwable $th) {
            CustomUtils::xzxDebug($th->getMessage());
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::EDIT_FAIL));
        }

        //SEND MAIL
        $sendMailParam = array(
            'type'  => 19,
            'email' => $info['newEmail'],
            "mchNo" => $mch['mchNo'],
        );
        CustomUtils::xzxDebug($sendMailParam);
        $this->EmailService->addMq($sendMailParam);

        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE),CustomUtils::xzxEmptyObject());
    }

    public function list(array $param)
    {

        $limit = $param['limit'];
        $page = $param['page'];        

        $total = $this->MchRegEmailChangeApplyDao->getCount($param);
        $list = $this->MchRegEmailChangeApplyDao->getList($param, $page, $limit);

        foreach ($list as $k => $v) {
            (isset($v['createTime']) && !empty($v['createTime'])) && $v['createTime'] = DateUtils::getDateTime($v['createTime']);
            (isset($v['reviewTime']) && !empty($v['reviewTime'])) && $v['reviewTime'] = DateUtils::getDateTime($v['reviewTime']);
            $v['strReviewStatus'] = CommonReviewStatusEnum::getLabel($v['reviewStatus']);
            $list[$k] = $v;
        }

        $data = array(
            'total' => $total,
            'list' => $list
        );

        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE),$data);
    }    
}